<!--
 * @Author: jiangjinchi 1468922694@qq.com
 * @Date: 2023-10-31 08:40:35
 * @LastEditors: jiangjinchi 1468922694@qq.com
 * @LastEditTime: 2023-12-12 16:04:20
 * @FilePath: \vue-admin-template\src\App.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div id="app">
    <router-view />
    <div>
      <el-dialog :close-on-click-modal="false" class="passDialog" title="" :visible.sync="dialogPassword" width="450px"
        :before-close="handleClose">
        <div style="width: 100%;text-align: center;margin-bottom: 30px;">
          <span style="font-size: 20px;color: #3F3F3F;">{{ !showNew ? '二级验证' : '输入密码' }}</span>
        </div>
        <el-form ref="form" :model="form" label-width="100px">
          <el-form-item label="输入密码" v-if="!showNew">
            <el-input style="width: 90%;" show-password v-model="form.oldPassword"></el-input>
          </el-form-item>
          <el-form-item label="输入新密码" v-else>
            <el-input style="width: 90%;" show-password v-model="form.newPassord"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button v-if="!showNew" :loading="loading" type="primary" @click="save">确 定</el-button>
          <el-button v-else :loading="loading" type="primary" @click="saveForm">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import Cookies from 'js-cookie';
export default {
  name: 'App',
  data() {
    return {
      form: {
        oldPassword: '',
        newPassord: '',
      },
      showNew: false,
      loading: false
    }
  },
  computed: {
    dialogPassword() {
      return this.$store.getters.dialogPassword
    }
  },
  watch: {
    dialogPassword() {
      this.showNew = false;
      this.form = {
        oldPassword: '',
        newPassord: '',
      }
    }
  },
  methods: {
    handleClose() {
      this.$store.commit('user/SET_PASSORD', false);
    },
    getReset() { // 重置密码
      this.$baseRequest('/sys/user/resetPwd/' + this.$store.getters.passwordId, {}, {}, 'put').then(res => {
        this.$message.success('重置密码成功');
        this.$store.commit('user/SET_PASSORD', false);
        this.$store.commit('user/SET_PASSORDID', "");
        this.loading = false;
      })
    },
    saveForm() { // 修改密码
      if (this.form.newPassord == undefined || this.form.newPassord == null || this.form.newPassord == "") {
        this.$message.warning('请输入密码');
        return;
      }
      this.loading = true;
      let names = Cookies.get('tokenNameEqu');
      let value = Cookies.get('tokenValueEqu');
      let params = {
        'Content-Type': 'application/json; charset=utf-8'
      };
      params[names] = value;
      let that = this;
      axios({
        method: 'put',
        url: that.$http + '/sys/user/revisePwd',
        headers: params,
        data: JSON.stringify(that.form.newPassord)
      }).then(function (response) {
        if (response.data.code == 200) {
          that.$message.success('修改成功');
          setTimeout(() => {
            that.$store.dispatch('user/resetToken').then(res => {
              that.$router.push(`/login?redirect=${that.$route.fullPath}`)
            })
          }, 1500);
        } else {
          that.$message.warning('修改失败，请重试');
        };
        that.loading = false;
      }).catch(function (error) {
        that.loading = false;
      })
    },
    save() { // 二次验证
      if (this.form.oldPassword == undefined || this.form.oldPassword == null || this.form.oldPassword == "") {
        this.$message.warning('请输入密码');
        return;
      }
      this.loading = true;
      let names = Cookies.get('tokenNameEqu');
      let value = Cookies.get('tokenValueEqu');
      let params = {
        'Content-Type': 'application/json; charset=utf-8'
      };
      params[names] = value;
      let that = this;
      axios({
        method: 'post',
        url: that.$http + '/openSafe',
        headers: params,
        data: JSON.stringify(that.form.oldPassword)
      }).then(function (response) {
        if (response.data.code == 200) {
          if (that.$store.getters.passwordId) { // 重置密码
            that.getReset();
            
            return;
          };
          that.showNew = true;
        } else {
          that.$message.warning('密码输入错误');
        };
        that.loading = false;
      }).catch(function (error) {
        that.loading = false;
      })


    }
  }
}
</script>
<style>
/*解决固定列遮盖底部滚定条导致滚动条无法滚动的问题*/
.el-table__body-wrapper{
            z-index: 2
        }
.el-table .warning-row {
  background: #FDE2E2;
}

.el-table .forewarning-row {
  background: oldlace;
}

.el-table .waiting-row {
  background: #E1F3D8;
}

.el-table {
  font-family: "SourceHanSansSC-Normal";
}

html,
body {
  font-family: "SourceHanSansSC-Normal";
}

.el-input__inner {
  height: 35px;
  line-height: 35px;
}

.el-date-editor .el-range-separator {
  line-height: 28px;
}

.el-date-editor .el-range__icon {
  line-height: 30px;
}

.el-date-editor .el-range__close-icon {
  list-style: 30px;
}

.el-table td,
.el-table th {
  padding: 5px 0px;
}

.el-table .cell {
  padding-left: 5px;
  padding-right: 5px;
}

.el-table thead td,
.el-table th {
  padding: 10px 0px;
}

.el-tooltip__popper {
  font-size: 13px;
  max-width: 40%
}

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}

input[type="number"] {
  -moz-appearance: textfield;
}

.passDialog .el-dialog__body {
  padding-top: 15px;
}

</style>